using cjx.ProxySocket;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
internal sealed class Class4 : Class3
{
	private string string_1;
	private byte[] byte_1;
	public Class4(Socket socket_1) : this(socket_1, "")
	{
	}
	public Class4(Socket socket_1, string string_2) : this(socket_1, string_2, "")
	{
	}
	public Class4(Socket socket_1, string string_2, string string_3) : base(socket_1, string_2)
	{
		this.method_26(string_3);
	}
	private void method_13()
	{
		base.method_3().Send(new byte[]
		{
			5,
			2,
			0,
			2
		});
		byte[] array = base.method_2(2);
		if (array[1] == 255)
		{
			throw new ProxyException("No authentication method accepted.");
		}
		Class0 @class;
		switch (array[1])
		{
		case 0:
			@class = new Class1(base.method_3());
			goto IL_7E;
		case 2:
			@class = new Class2(base.method_3(), base.method_5(), this.method_25());
			goto IL_7E;
		}
		throw new ProtocolViolationException();
		IL_7E:
		@class.vmethod_0();
	}
	private byte[] method_14(string string_2, int int_0)
	{
		if (string_2 == null)
		{
			throw new ArgumentNullException();
		}
		if (int_0 <= 0 || int_0 > 65535 || string_2.Length > 255)
		{
			throw new ArgumentException();
		}
		byte[] array = new byte[7 + string_2.Length];
		array[0] = 5;
		array[1] = 1;
		array[2] = 0;
		array[3] = 3;
		array[4] = (byte)string_2.Length;
		Array.Copy(Encoding.ASCII.GetBytes(string_2), 0, array, 5, string_2.Length);
		Array.Copy(base.method_0(int_0), 0, array, string_2.Length + 5, 2);
		return array;
	}
	private byte[] method_15(IPEndPoint ipendPoint_0)
	{
		if (ipendPoint_0 == null)
		{
			throw new ArgumentNullException();
		}
		byte[] array = new byte[10];
		array[0] = 5;
		array[1] = 1;
		array[2] = 0;
		array[3] = 1;
		Array.Copy(base.method_1(ipendPoint_0.Address.Address), 0, array, 4, 4);
		Array.Copy(base.method_0(ipendPoint_0.Port), 0, array, 8, 2);
		return array;
	}
	public override void vmethod_0(string string_2, int int_0)
	{
		this.method_16(this.method_14(string_2, int_0));
	}
	public override void vmethod_1(IPEndPoint ipendPoint_0)
	{
		this.method_16(this.method_15(ipendPoint_0));
	}
	private void method_16(byte[] byte_2)
	{
		this.method_13();
		base.method_3().Send(byte_2);
		byte[] array = base.method_2(4);
		if (array[1] != 0)
		{
			base.method_3().Close();
			throw new ProxyException((int)array[1]);
		}
		switch (array[3])
		{
		case 1:
			array = base.method_2(6);
			return;
		case 3:
			array = base.method_2(1);
			array = base.method_2((int)(array[0] + 2));
			return;
		case 4:
			array = base.method_2(18);
			return;
		}
		base.method_3().Close();
		throw new ProtocolViolationException();
	}
	public override Class6 vmethod_3(string string_2, int int_0, Delegate0 delegate0_1, IPEndPoint ipendPoint_0)
	{
		this.delegate0_0 = delegate0_1;
		this.method_28(this.method_14(string_2, int_0));
		base.method_3().BeginConnect(ipendPoint_0, new AsyncCallback(this.method_17), base.method_3());
		base.method_8(new Class6());
		return base.method_7();
	}
	public override Class6 vmethod_2(IPEndPoint ipendPoint_0, Delegate0 delegate0_1, IPEndPoint ipendPoint_1)
	{
		this.delegate0_0 = delegate0_1;
		this.method_28(this.method_15(ipendPoint_0));
		base.method_3().BeginConnect(ipendPoint_1, new AsyncCallback(this.method_17), base.method_3());
		base.method_8(new Class6());
		return base.method_7();
	}
	private void method_17(IAsyncResult iasyncResult_0)
	{
		try
		{
			base.method_3().EndConnect(iasyncResult_0);
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			base.method_3().BeginSend(new byte[]
			{
				5,
				2,
				0,
				2
			}, 0, 4, SocketFlags.None, new AsyncCallback(this.method_18), base.method_3());
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private void method_18(IAsyncResult iasyncResult_0)
	{
		try
		{
			base.method_3().EndSend(iasyncResult_0);
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			base.method_10(new byte[1024]);
			base.method_12(0);
			base.method_3().BeginReceive(base.method_9(), 0, base.method_9().Length, SocketFlags.None, new AsyncCallback(this.method_19), base.method_3());
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private void method_19(IAsyncResult iasyncResult_0)
	{
		try
		{
			base.method_12(base.method_11() + base.method_3().EndReceive(iasyncResult_0));
			if (base.method_11() <= 0)
			{
				throw new SocketException();
			}
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			if (base.method_11() < 2)
			{
				base.method_3().BeginReceive(base.method_9(), base.method_11(), base.method_9().Length - base.method_11(), SocketFlags.None, new AsyncCallback(this.method_19), base.method_3());
			}
			else
			{
				Class0 @class;
				switch (base.method_9()[1])
				{
				case 0:
					@class = new Class1(base.method_3());
					goto IL_D7;
				case 2:
					@class = new Class2(base.method_3(), base.method_5(), this.method_25());
					goto IL_D7;
				}
				this.delegate0_0(new SocketException());
				return;
				IL_D7:
				@class.vmethod_1(new Delegate0(this.method_20));
			}
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private void method_20(Exception exception_0)
	{
		if (exception_0 != null)
		{
			this.delegate0_0(exception_0);
			return;
		}
		try
		{
			base.method_3().BeginSend(this.method_27(), 0, this.method_27().Length, SocketFlags.None, new AsyncCallback(this.method_21), base.method_3());
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
		}
	}
	private void method_21(IAsyncResult iasyncResult_0)
	{
		try
		{
			base.method_3().EndSend(iasyncResult_0);
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			base.method_10(new byte[5]);
			base.method_12(0);
			base.method_3().BeginReceive(base.method_9(), 0, base.method_9().Length, SocketFlags.None, new AsyncCallback(this.method_22), base.method_3());
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private void method_22(IAsyncResult iasyncResult_0)
	{
		try
		{
			base.method_12(base.method_11() + base.method_3().EndReceive(iasyncResult_0));
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			if (base.method_11() == base.method_9().Length)
			{
				this.method_23(base.method_9());
			}
			else
			{
				base.method_3().BeginReceive(base.method_9(), base.method_11(), base.method_9().Length - base.method_11(), SocketFlags.None, new AsyncCallback(this.method_22), base.method_3());
			}
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private void method_23(byte[] byte_2)
	{
		switch (byte_2[3])
		{
		case 1:
			base.method_10(new byte[5]);
			goto IL_4D;
		case 3:
			base.method_10(new byte[(int)(byte_2[4] + 2)]);
			goto IL_4D;
		case 4:
			byte_2 = new byte[17];
			goto IL_4D;
		}
		throw new ProtocolViolationException();
		IL_4D:
		base.method_12(0);
		base.method_3().BeginReceive(base.method_9(), 0, base.method_9().Length, SocketFlags.None, new AsyncCallback(this.method_24), base.method_3());
	}
	private void method_24(IAsyncResult iasyncResult_0)
	{
		try
		{
			base.method_12(base.method_11() + base.method_3().EndReceive(iasyncResult_0));
		}
		catch (Exception exception_)
		{
			this.delegate0_0(exception_);
			return;
		}
		try
		{
			if (base.method_11() == base.method_9().Length)
			{
				this.delegate0_0(null);
			}
			else
			{
				base.method_3().BeginReceive(base.method_9(), base.method_11(), base.method_9().Length - base.method_11(), SocketFlags.None, new AsyncCallback(this.method_24), base.method_3());
			}
		}
		catch (Exception exception_2)
		{
			this.delegate0_0(exception_2);
		}
	}
	private string method_25()
	{
		return this.string_1;
	}
	private void method_26(string string_2)
	{
		if (string_2 == null)
		{
			throw new ArgumentNullException();
		}
		this.string_1 = string_2;
	}
	private byte[] method_27()
	{
		return this.byte_1;
	}
	private void method_28(byte[] byte_2)
	{
		this.byte_1 = byte_2;
	}
}
